package com.termux.app;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.util.*;
import com.termux.util.*;

public class GlesActivity extends Activity {
    private GLSurfaceView m_view;
	NativeWrapper wrapper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		LibHelperUtils.setContext(this);
		//LibHelperUtils.loadLibraryFile
		System.load(getIntent().getStringExtra("fname"));
        m_view = new GLApp(getApplication(),wrapper);
        setContentView(m_view);
    }

    @Override
    protected void onPause ()
    {
        super.onPause();
        m_view.onPause();
    }

    @Override
    protected void onResume ()
    {
        super.onResume();
        m_view.onResume();
    }

	@Override
	protected void onDestroy()
	{
		// TODO: Implement this method
		Log.d("opengl","ondestroy");
		wrapper.nativeDestroy();
		super.onDestroy();
		//System.exit(0);
	}
	
}

class GLApp extends GLSurfaceView
{
    GLSurfaceView.Renderer m_renderer;
    public GLApp(Context context,NativeWrapper wrapper) {
        super(context);
        setEGLContextClientVersion(2);
        m_renderer = new GLAppRenderer(wrapper);
        setRenderer(m_renderer);
    }
}

class GLAppRenderer implements GLSurfaceView.Renderer
{
    NativeWrapper wrapper;
	public GLAppRenderer(NativeWrapper wrapper){
		this.wrapper = wrapper;
	}
    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
		
        wrapper.nativeInit();
    }

    @Override
    public void onSurfaceChanged(GL10 gl10, int w, int h) {
        wrapper.nativeResize(w, h);
    }

    @Override
    public void onDrawFrame(GL10 gl10) {
        wrapper.nativeDraw();
    }
}


